package com.bigger.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bigger.constant.ErrorConstant;
import com.bigger.context.BaseContext;
import com.bigger.dto.UpdateAddressDTO;
import com.bigger.entity.AddressBook;
import com.bigger.exception.AddressException;
import com.bigger.exception.PowerException;
import com.bigger.mapper.AddressBookMapper;
import com.bigger.service.AddressService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressBookMapper addressBookMapper;
    @Override
    public List<AddressBook> get() {
        QueryWrapper<AddressBook> wrapper=new QueryWrapper<AddressBook>()
                .eq("user_id", BaseContext.getCurrentId());
        List<AddressBook> addressBooks = addressBookMapper.selectList(wrapper);
        return addressBooks;
    }

    @Override
    public void update(UpdateAddressDTO updateAddressDTO) {
        //这里可以添加验证这个地址簿是不是本id用户修改，感觉没必要就没写了
        checkParam(updateAddressDTO);
        AddressBook addressBook=new AddressBook();
        BeanUtils.copyProperties(updateAddressDTO,addressBook);
        addressBookMapper.updateById(addressBook);
    }

    @Override
    public void add(UpdateAddressDTO updateAddressDTO) {
        checkParam(updateAddressDTO);
        QueryWrapper<AddressBook> wrapper=new QueryWrapper<AddressBook>()
                .eq("user_id",BaseContext.getCurrentId());
        Long total = addressBookMapper.selectCount(wrapper);
        if (total>=3){ //有3个地址就够用了，要这么多干嘛
            throw new AddressException(ErrorConstant.ADDRESS_IS_FULL);
        }
        AddressBook addressBook=new AddressBook();
        BeanUtils.copyProperties(updateAddressDTO,addressBook);
        addressBook.setId(null);
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookMapper.insert(addressBook);
    }

    @Override
    public void delete(Long id) {
        addressBookMapper.deleteById(id);
    }

    @Override
    public AddressBook getById(Long id) {
        AddressBook addressBook = addressBookMapper.selectById(id);
        if (!addressBook.getUserId().equals(BaseContext.getCurrentId())){ //查询涉及用户隐私问题所以进行了判断
            throw new PowerException(ErrorConstant.INSUFFICIENT_PRIVILEGES);
        }
        return addressBook;
    }

    private void checkParam(UpdateAddressDTO updateAddressDTO) {
        if (!updateAddressDTO.getPhone().matches("1[3-9]\\d{9}")) {//电话号码正则校验
            throw new AddressException(ErrorConstant.PHONE_ERROR);
        }
        if(StringUtils.isBlank(updateAddressDTO.getName())){
            throw new AddressException(ErrorConstant.NAME_IS_EMPTY);
        }
        if(StringUtils.isBlank(updateAddressDTO.getAddress())){
            throw new AddressException(ErrorConstant.ADDRESS_IS_EMPTY);
        }
    }
}
